﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;

namespace SFast
{
    public static class MyUrl
    {
        #region 把~/转换为绝对路径
        /// <summary>
        /// 把~/转换为绝对路径
        /// </summary>
        /// <param name="url">带有~/的URL</param>
        /// <returns></returns>
        public static string ToAb(string url)
        {
            string urlPath, urlPar;  //url地址(?号之前)  url参数(?号之后)
            if (url.IndexOf("~/") != -1)
            {
                urlPath = url.IndexOf("?") != -1 ? url.Split('?')[0] : url;   //?号前url
                urlPar = url.Split('?').Length > 1 ? url.Split('?')[1] : ""; //？后参数
                url = VirtualPathUtility.ToAbsolute(urlPath) + (String.IsNullOrEmpty(urlPar) ? "" : "?" + urlPar);
            }
            return url;
        }
        #endregion

        #region 拼接出全整url
        /// <summary>
        /// 拼接出全整url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string AllUrl(string url)
        {
            string host = HttpContext.Current.Request.Url.Host;
            return "http://" + host + url;
        }
        #endregion

        #region url编码解码
        /// <summary>
        /// url解码,并返回已解码的字符串
        /// </summary>
        /// <param name="s"></param>
        public static string UrlDecode(string s)
        {
            return HttpContext.Current.Server.UrlDecode(s);
        }

        /// <summary>
        /// url编码,并返回已编码的字符串
        /// </summary>
        /// <param name="s"></param>
        public static string UrlEncode(string s)
        {
            return HttpContext.Current.Server.UrlEncode(s);
        }
        #endregion

        #region 获取URL中的参数的值，当为null 返回空字符串
        /// <summary>
        /// 获取URL中的参数的值，当为null 返回空字符串
        /// </summary>
        /// <param name="key">url 参数 键</param>
        /// <returns></returns>
        public static String Request(String key)
        {
            return Request(key, "");
        }
        #endregion

        #region  获取URL中的参数的值，当为null时返回设置的默认值
        /// <summary>
        /// 获取URL中的参数的值，当为null时返回设置的默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="Default"></param>
        /// <returns></returns>
        public static String Request(String key, String Default)
        {
            var Request = HttpContext.Current.Request;
            String val = Request.QueryString[key];
            if (String.IsNullOrEmpty(val))
            {
                val = Default;
            }
            return val;
        }
        #endregion

        #region 返回请求的浏览器名字以及版本
        /// <summary>
        /// 返回请求的浏览器名字以及版本
        /// ie: ie7,ie8...
        /// 火狐：firefox11.0 等
        /// 谷歌：desktop
        /// </summary>
        /// <returns></returns>
        public static String GetBrowser()
        {
            return HttpContext.Current.Request.Browser.Type.ToLower();
        }
        #endregion
    }
}
